﻿#pragma execution_character_set("utf-8")

#include "xunfei_iat.h"
#include <QMessageBox>
#include <QDebug>
#define syslog qDebug()

Xunfei_iat::Xunfei_iat(QObject *parent) : QObject(parent)
{
    QFile file(QCoreApplication::applicationDirPath() + "/appid.txt");
    if(file.exists())
    {
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        if(file.size() > 0)
        {
            app_id = QString(file.readAll());
        }
        file.close();
    }

    wsClient = new WSClient(this);
    connect(wsClient, &WSClient::NewTextMessage, this, &Xunfei_iat::parseTextMsg);
    connect(wsClient, &WSClient::NewBinaryMessage, this, &Xunfei_iat::parseBinaryMsg);
    connect(wsClient, &WSClient::occur_error, this, &Xunfei_iat::ws_error);

    audiofile.setFileName(QCoreApplication::applicationDirPath() + "/temp.raw");

    commonArgs.insert("app_id",app_id);
    BusinessArgs.insert("language","zh_cn");
    BusinessArgs.insert("domain","iat");
    BusinessArgs.insert("accent","mandarin");

}

void Xunfei_iat::sendAudio()
{
    if(app_id.isEmpty())
    {
        QMessageBox::warning(0,"","appid为空，请在appid.txt内写入后重新打开应用程序");
        return;
    }
    syslog<<app_id;
    if(!audiofile.open(QIODevice::ReadOnly))
    {
        syslog<<audiofile.errorString();
        return;
    }
    qint64 fsize = audiofile.size();
    syslog<<"file size is "<<fsize<<" bytes";
    int framsize = 8000;
    int status = 0;
    QJsonDocument doc;
    QJsonObject msg;
    QJsonObject data;

    data.insert("format","audio/L16;rate=8000");
    data.insert("encoding","raw");
    while(1)
    {
        data.insert("status",status);
        if(status == 0)
        {
            data.insert("audio", QString(audiofile.read(framsize).toBase64()));    //需要base64编码
            msg.insert("common", commonArgs);
            msg.insert("business", BusinessArgs);
            msg.insert("data",data);
            doc.setObject(msg);
            wsClient->sendBinaryMessage(doc.toJson(QJsonDocument::Indented));

            msg.remove("common");
            msg.remove("business");
            fsize -= 8000;
            status = 1; //转到中间帧
            QThread::msleep(40);
        }
        else if(status == 1)
        {
            if(fsize > 0)
            {
                data.insert("audio", QString(audiofile.read(framsize).toBase64()));
                msg.insert("data",data);
                doc.setObject(msg);
                wsClient->sendBinaryMessage(doc.toJson(QJsonDocument::Indented));
                fsize -= 8000;
                QThread::msleep(40);
            }
            else
            {
                data.remove("audio");
                data.remove("format");
                data.remove("encoding");
                status = 2;
            }
        }
        else if(status == 2)
        {
            msg.insert("data",data);
            doc.setObject(msg);
            wsClient->sendBinaryMessage(doc.toJson(QJsonDocument::Indented));
            break;
        }
    }
    audiofile.close();
}

void Xunfei_iat::parseTextMsg(const QString& content)
{
    wsClient->Disconnect();
    QJsonDocument doc = QJsonDocument::fromJson(content.toUtf8());
    if(doc.object().value("code").toInt() != 0)
    {
        emit msg_to_ui(content);
    }
    else
    {
        QJsonObject result = doc.object().take("data").toObject().take("result").toObject(); //result object
        QJsonArray ws = result.value("ws").toArray();
        QString resultstr = "";
        for(int index = 0; index < ws.size(); index++)
        {
            resultstr += ws.at(index).toObject().value("cw").toArray().at(0).toObject().value("w").toString();
        }
        emit msg_to_ui("识别结果：" + resultstr);
    }
}

void Xunfei_iat::parseBinaryMsg(const QByteArray& data)
{
    emit msg_to_ui(QString(data));
}
void Xunfei_iat::ws_error(const QString& content)
{
    emit msg_to_ui(content);
}
